package cn.ycl.system.other.study.leetCode;

public class MyCircularDeque {
    int[] cur;
    //当前头部
    int pre;
    //当前尾部
    int tail;

    public static void main(String[] args) {
        MyCircularDeque myCircularDeque = new MyCircularDeque(3);
        myCircularDeque.print(myCircularDeque.cur);
        boolean b = myCircularDeque.insertFront(8);
        myCircularDeque.print(myCircularDeque.cur);
        boolean b1 = myCircularDeque.insertLast(8);
        myCircularDeque.print(myCircularDeque.cur);
        boolean b2 = myCircularDeque.insertLast(2);
        myCircularDeque.print(myCircularDeque.cur);
        int front = myCircularDeque.getFront();
        myCircularDeque.print(myCircularDeque.cur);
        boolean b3 = myCircularDeque.deleteLast();
        myCircularDeque.print(myCircularDeque.cur);
        int rear = myCircularDeque.getRear();
        myCircularDeque.print(myCircularDeque.cur);
        boolean b4 = myCircularDeque.insertFront(9);
        myCircularDeque.print(myCircularDeque.cur);
        boolean b5 = myCircularDeque.deleteFront();
        myCircularDeque.print(myCircularDeque.cur);
        int rear1 = myCircularDeque.getRear();
        myCircularDeque.print(myCircularDeque.cur);
        boolean b6 = myCircularDeque.insertLast(2);
        myCircularDeque.print(myCircularDeque.cur);
        boolean full = myCircularDeque.isFull();
        myCircularDeque.print(myCircularDeque.cur);
        System.out.println("["+ "null," + b + ","+ b1 + ","+ b2 + ","+ front + ","+ b3 + ","+ rear + ","+ b4 + ","+ b5 + ","+ rear1 + ","+ b6 + ","+ full + "]");
    }

    public void print(int[] cur){
        for (int i : cur) {
            System.out.print("\t" + i);
        }
        System.out.println();
    }

    public MyCircularDeque(int k) {
        cur = new int[k];
        pre = -1;
        tail = k;
    }
    
    public boolean insertFront(int value) {
        //队列满了就不插入数据
        if (isFull()) {
            return false;
        } else {
            if(pre == -1){
                pre++;
                cur[pre] = value;
            }else {
                int temp = pre;
                while (temp >= 0) {
                    cur[temp + 1] = cur[temp];
                    temp--;
                }
                cur[0] = value;
                pre++;
            }
            return true;
        }
        //34   【8,8,2】【8,8】
    }
    
    public boolean insertLast(int value) {
        //队列满了就不插入数据
        if (isFull()) {
            return false;
        } else {
            if (tail == cur.length) {
                cur[cur.length - 1] = value;
                tail--;
            } else {
                int temp = tail;
                while (temp < cur.length - 1) {
                    cur[temp - 1] = cur[temp];
                    temp++;
                }
                cur[cur.length - 1] = value;
                tail--;
            }
            return true;
        }
    }
    
    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        } else {
            if (pre == -1) {
                cur[tail] = 0;
                tail++;
            } else {
                int temp = pre;
                int i = 0;
                while (i < temp) {
                    cur[i] = cur[i + 1];
                    i++;
                }
                cur[pre] = 0;
                pre--;
            }
            return true;
        }
    }
    
    public boolean deleteLast() {
        if(isEmpty()){
            return false;
        }else {
            if(tail == cur.length){
                cur[pre] = 0;
                pre--;
            }else {
                int temp = cur.length - 1;
                while (temp > tail) {
                    cur[temp] = cur[temp - 1];
                    temp--;
                }
                cur[tail] = 0;
                tail++;
            }
            return true;
        }
    }
    
    public int getFront() {
        if(isEmpty()){
            return -1;
        }else{
            if(pre == -1){
                return cur[tail];
            }else{
                return cur[0];
            }
        }
    }
    
    public int getRear() {
        if(isEmpty()){
            return -1;
        }else{
            if(tail == cur.length){
                return cur[pre];
            }else{
                return cur[cur.length - 1];
            }
        }
    }
    
    public boolean isEmpty() {
        return pre == -1 && tail == cur.length;
    }
    
    public boolean isFull() {
        return (tail - pre) == 1;
    }
}
